package memory

import (
	"booking-service/internal/models"
	"errors"
	"sync"
	"time"
)

type InMemoryRoomAvailabilityStorage struct {
	availability map[string]models.RoomAvailability
	mu           sync.Mutex
}

func NewInMemoryRoomAvailabilityStorage() *InMemoryRoomAvailabilityStorage {
	return &InMemoryRoomAvailabilityStorage{
		availability: make(map[string]models.RoomAvailability),
	}
}

func (s *InMemoryRoomAvailabilityStorage) GetAvailability(hotelID, roomID string, date time.Time) (models.RoomAvailability, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	key := hotelID + roomID + date.Format("2006-01-02")
	if availability, exists := s.availability[key]; exists {
		return availability, nil
	}
	return models.RoomAvailability{}, errors.New("room not available")
}

func (s *InMemoryRoomAvailabilityStorage) UpdateAvailability(hotelID, roomID string, date time.Time, quota int) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	key := hotelID + roomID + date.Format("2006-01-02")
	s.availability[key] = models.RoomAvailability{
		HotelID: hotelID,
		RoomID:  roomID,
		Date:    date,
		Quota:   quota,
	}
	return nil
}
